//
//  KDCollectTypeListController.m
//  WangJingApp
//
//  Created by 魏强 on 17/11/8.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDCollectTypeListController.h"
#import "KDCollectRequestTool.h"
#import "KDFavorTypeListResult.h"
#import "KDCollectCellModel.h"
#import "KDMoveFavorParam.h"
#import "UIViewController+BackButtonHandler.h"
@interface KDCollectTypeListController ()<UITableViewDelegate, UITableViewDataSource,BackButtonHandlerProtocol>
/**  <#Description#> */
@property (nonatomic, weak) UITableView *tableView;
/**  <#Description#> */
@property (nonatomic, strong) NSArray *infoArr;
/**  底部的提示label */
@property (nonatomic, weak) UILabel *noticeLb;

/**  <#Description#> */
@property (nonatomic, assign) NSInteger selectIndex;


@end
static const CGFloat confirmBtnH = 44.0;
@implementation KDCollectTypeListController
#pragma mark - 懒加载区
- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSArray array] ;
    }
    return _infoArr;
}

- (void)setSelectIndex:(NSInteger)selectIndex{
    //
    UITableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectIndex inSection:0]];
    oldCell.textLabel.textColor = [UIColor blackColor];
    oldCell.imageView.image = [UIImage imageNamed:@"favorite"];
    oldCell.accessoryView.hidden = YES;
    
    KDFavorType *old = self.infoArr[_selectIndex];
    old.check = NO;
    
    
    UITableViewCell *newCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0]];
    KDFavorType *new = self.infoArr[selectIndex];
    new.check = YES;
    newCell.textLabel.textColor = KDSelectColor;
    newCell.imageView.image = [UIImage imageNamed:@"favorite_s"];
    newCell.accessoryView.hidden = NO;
    _selectIndex = selectIndex;
    
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
    self.title = @"我的收藏夹";
    [self setupNav];
    [self loadData];
    
}

- (void)moveSuccess:(MoveSuccessBlock)block{
    self.moveSuccessBlock = block;
}
#pragma mark - 板块布置区
- (void)setupNav{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(rightItemClick)];

}
- (void)setupNoticeLb{
    UILabel *noticeLb = [[UILabel alloc]init];
    self.noticeLb = noticeLb;
    noticeLb.frame = CGRectMake(0, 0, screenW, screenH);
    noticeLb.text = @"本收藏夹为空";
    noticeLb.layer.borderColor = BackGroundColor.CGColor;
    noticeLb.layer.borderWidth = 0.5;
    noticeLb.backgroundColor = [UIColor clearColor];
    noticeLb.textColor = [UIColor whiteColor];
    noticeLb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noticeLb];
    
}
- (void)setupTableview{
    [self check];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , 0, screenW,  screenH - 64 - confirmBtnH) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
//    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    [self setupConfirmBtn];
}
- (void)check{
    NSInteger i = 0;
    for (KDFavorType *model in self.infoArr) {
        if (model.type_id == self.collectCellModel.type_id) {
            model.check = YES;
            self.selectIndex = i;
            return;
        }
        i++;
    }
}

- (void)setupConfirmBtn{
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(0, screenH - confirmBtnH - 64, screenW, confirmBtnH);
    [self.view addSubview:confirmBtn];
    KDLog(@"%@",NSStringFromCGRect(confirmBtn.frame));
    [confirmBtn setTitle:@"确定移动" forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.backgroundColor = KDSelectColor;
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
}
#pragma mark - 事件点击区
- (void)rightItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)confirmBtnClick:(UIButton *)confirmBtn{
    WS(ws);
    KDMoveFavorParam *param = [[KDMoveFavorParam alloc]init];
    param.ID = [NSString stringWithFormat:@"%ld", self.collectCellModel.ID];
    KDFavorType *type = self.infoArr[self.selectIndex];
    param.type_id = [NSString stringWithFormat: @"%ld",type.type_id];
    
    [KDCollectRequestTool moveFavorWithParam:param success:^(KDBaseResult *responseObejct) {
        
        [SVProgressHUD showSuccessWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        if ([responseObejct.code isEqualToString:@"1"]) {
            if (ws.moveSuccessBlock != nil) {
                ws.moveSuccessBlock();
            }
            [ws.navigationController popViewControllerAnimated:YES];
            
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"网络错误!" maskType:SVProgressHUDMaskTypeGradient];

    }];
}
#pragma mark - 网络请求区
- (void)loadData{
    WS(ws);
    KDBaseParam *param = [[KDBaseParam alloc]init];
    [KDCollectRequestTool getFavorTypeListWithParam:param success:^(KDFavorTypeListResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            ws.infoArr = responseObejct.data;
            if (ws.infoArr.count) {
                [ws setupTableview];
            }else{
                [ws setupNoticeLb];
            }
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
    }];
}
#pragma mark - 通知接收区


#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId =@"cellName";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
//        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selectedBackgroundView = [[UIView alloc]init];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius = 3;
        btn.clipsToBounds = YES;
        btn.frame = CGRectMake(0, 0, 30, 16);
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitle:@"当前" forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(<#btn#>Click:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = KDSelectColor;
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cell.accessoryView = btn;
    }
    KDFavorType *source = self.infoArr[indexPath.row];
    cell.textLabel.text = source.type_name;
    if (source.check) {
        cell.accessoryView.hidden = NO;
        cell.textLabel.textColor = KDSelectColor;
        cell.imageView.image = [UIImage imageNamed:@"favorite_s"];
    }else{
        cell.accessoryView.hidden = YES;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.imageView.image = [UIImage imageNamed:@"favorite"];
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIndex = indexPath.row;
    
}
#pragma mark - BackButtonHandlerProtocol



@end
